// Copyright 2009 StarCite Inc.  All rights reserved.
// MongoDBTest.java 3:13:31 PM danny.liu
package com.billowsoft.sample.mongodb;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.bson.BSONObject;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MapReduceCommand.OutputType;
import com.mongodb.MapReduceOutput;
import com.mongodb.Mongo;

/**
 * @author danny.liu Jun 18, 2012
 * @version 1.0
 * 
 */
public class MongoDBTest {

	public static void main(String[] args) throws Exception{
		Mongo mongo = new Mongo("localhost", 27017);
		
		DB testDB = mongo.getDB("test");
		
		DBCollection coll = testDB.getCollection("testColl");
		if(coll == null){
			System.out.println("Coll is null.");
			return;
		}
		
		BasicDBObject doc = new BasicDBObject();
		doc.put("name", "MongoDB2");
		doc.put("type", "database");
		doc.put("count", 3);
		
		BasicDBObject info = new BasicDBObject();
		info.put("x", 203);
		info.put("y", 102);
		doc.put("info", info);
		
		coll.insert(doc);
		
		Map<String, String> queries = new HashMap<String, String>();
		queries.put("name", "MongoDB33");
		String map = "function Map() { emit(this.name, this.count);}";
		String reduce = "function (key, values) { return Math.max.apply(Math, values); }";
		MapReduceOutput output = coll.mapReduce(map, reduce, null, OutputType.INLINE, new BasicDBObject(queries));
		System.out.println(output.getCommandResult().get("results"));
	}
}

class TestDocument implements DBObject{
	
	private String name;
	private int salary;
	
	/**
	 * @param name
	 * @param salary
	 */
	public TestDocument(String name, int salary) {
		this.name = name;
		this.salary = salary;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}

	/* (non-Javadoc)
	 * @see org.bson.BSONObject#containsField(java.lang.String)
	 */
	@Override
	public boolean containsField(String arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.bson.BSONObject#containsKey(java.lang.String)
	 */
	@Override
	public boolean containsKey(String arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see org.bson.BSONObject#get(java.lang.String)
	 */
	@Override
	public Object get(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.bson.BSONObject#keySet()
	 */
	@Override
	public Set<String> keySet() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.bson.BSONObject#put(java.lang.String, java.lang.Object)
	 */
	@Override
	public Object put(String arg0, Object arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.bson.BSONObject#putAll(org.bson.BSONObject)
	 */
	@Override
	public void putAll(BSONObject arg0) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.bson.BSONObject#putAll(java.util.Map)
	 */
	@Override
	public void putAll(Map arg0) {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see org.bson.BSONObject#removeField(java.lang.String)
	 */
	@Override
	public Object removeField(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.bson.BSONObject#toMap()
	 */
	@Override
	public Map toMap() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.mongodb.DBObject#isPartialObject()
	 */
	@Override
	public boolean isPartialObject() {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see com.mongodb.DBObject#markAsPartialObject()
	 */
	@Override
	public void markAsPartialObject() {
		// TODO Auto-generated method stub
		
	}
	
}
